# coding: utf-8
from PySide6.QtCore import QPropertyAnimation, QEasingCurve, QPoint, Qt, Property
from PySide6.QtGui import QHideEvent, QShowEvent, QPaintEvent, QPainter, QColor
from PySide6.QtWidgets import QFrame
from qfluentwidgets import isDarkTheme


class SideWidget(QFrame):
    """
    侧边栏组件
    """
    Left = 0
    Right = 1
    Top = 2
    Bottom = 3

    def __init__(self, parent=None):
        super().__init__(parent)
        self.__direction = self.Left  # 侧边栏位置
        self.animation = QPropertyAnimation(self, b"pos", self)
        self.animation.setDuration(200)
        self.animation.setEasingCurve(QEasingCurve.Type.InOutQuart)

    def setDirection(self, direction: int):
        self.__direction = direction

    def getDirection(self) -> int:
        return self.__direction

    def hideEvent(self, event: QHideEvent):
        if self.direction == self.Left:
            self.animation.setStartValue(QPoint(0, 0))
            self.animation.setEndValue(QPoint(-self.width(), 0))
        elif self.direction == self.Right:
            self.animation.setStartValue(QPoint(self.width(), 0))
            self.animation.setEndValue(QPoint(0, 0))
        elif self.direction == self.Top:
            self.animation.setStartValue(QPoint(0, 0))
            self.animation.setEndValue(QPoint(0, -self.height()))
        elif self.direction == self.Bottom:
            self.animation.setStartValue(QPoint(0, self.height()))
            self.animation.setEndValue(QPoint(0, 0))
        self.animation.start()

    def showEvent(self, event: QShowEvent):
        if self.direction == self.Left:
            self.animation.setStartValue(QPoint(-self.width(), 0))
            self.animation.setEndValue(QPoint(0, 0))
        elif self.direction == self.Right:
            self.animation.setStartValue(QPoint(self.width(), 0))
            self.animation.setEndValue(QPoint(0, 0))
        elif self.direction == self.Top:
            self.animation.setStartValue(QPoint(0, -self.height()))
            self.animation.setEndValue(QPoint(0, 0))
        elif self.direction == self.Bottom:
            self.animation.setStartValue(QPoint(0, self.height()))
            self.animation.setEndValue(QPoint(0, 0))
        self.animation.start()

    def paintEvent(self, event: QPaintEvent):
        super().paintEvent(event)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        painter.setPen(Qt.PenStyle.NoPen)
        color = QColor(32, 32, 32, 100) if isDarkTheme() else QColor(240, 244, 249, 100)
        painter.fillRect(self.rect(), color)
        painter.end()

    direction = Property(int, getDirection, setDirection)
